package com.mall507.fivepeaks.activity.main;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.hzh.frame.widget.xwebview.XWebViewActivity;
import com.mall507.comn.model.ConvenientLifeQiuGouInfo;
import com.mall507.comn.model.Message;
import com.mall507.comn.model.MineMessage;
import com.mall507.comn.model.User;
import com.mall507.comn.tools.UserTools;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.community.CommunityAssociationInfoActivityRUI;
import com.mall507.fivepeaks.activity.comn.StretchyTextView;
import com.mall507.fivepeaks.widget.Banner.BGABanner;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.xdialog.XAddressDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.Parser;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/main/MainTourInfoRUI")
public class MainTourInfoRUI extends AbsRecyclerViewUI<Message> {
    private View headView, commentView;
    private List<String> listUrl;
    @ViewInject(R.id.collection) //收藏
    LinearLayout collection;
    @ViewInject(R.id.img_collection) //收藏图片
    ImageView img_collection;
    @ViewInject(R.id.lickNumber)
    TextView lickNumber;
    @ViewInject(R.id.send) //发送评论
    TextView send;
    @ViewInject(R.id.comment) //评论内容
    EditText comment;

    private int page = 0;
    private String recommend = ""; //为你推荐
    TextView dizhi;
    TextView jieshao;
    TextView luxian;
    TextView tuijian;
    private int flag = 0;
    private int flag2 = 0;
    private int flag3 = 0;
    String tourAddress = "";
    String way = "";
    String recommend_link = "";
    String tourlat = "";
    String tourlng = "";
    
    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_tour_info;
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_tour_item_info;
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected String setHttpPath() {
        return "Comment/getTourismComment";
    }

    @Override
    protected void bindView() {
        getTitleView().setContent("详情");
        showLoding();
        headView = getLayoutInflater().inflate(R.layout.head_rv_tour_info, null);
        getAdapter().setHeaderView(headView);
        dizhi = headView.findViewById(R.id.address);
        jieshao = headView.findViewById(R.id.desc);
        luxian = headView.findViewById(R.id.way);
        tuijian = headView.findViewById(R.id.recommend);
        tuijian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recommend_link != null && recommend_link.length()>0){
                    Intent in3 = new Intent(MainTourInfoRUI.this, XWebViewActivity.class);
                    in3.putExtra("url", recommend_link);
                    MainTourInfoRUI.this.startActivity(in3);
                }
            }
        });
        dizhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new XAddressDialog().show(MainTourInfoRUI.this, tourAddress,tourlng,tourlat, new XAddressDialog.XUserMainSignRewardDialogCallBack() {
                    @Override
                    public void confirm() {

                    }

                    @Override
                    public void cancel() {

                    }
                });
            }
        });
        jieshao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag2 == 0){
                    jieshao.setMaxLines(100);
                    flag2 = 1;
                } else if (flag2 == 1) {
                    jieshao.setMaxLines(1);
                    flag2 = 0;
                }
            }
        });
        luxian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (way != null && way.length()>0){
                    Intent in3 = new Intent(MainTourInfoRUI.this, XWebViewActivity.class);
//                    in3.putExtra("url", "https://mp.weixin.qq.com/s/11WjmlZNhQ38HctrMnwRNA");
                    in3.putExtra("url", way);
                    MainTourInfoRUI.this.startActivity(in3);
                }
            }
        });
        getComment();
        isCollection();
        collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserTools.getInstance().getIsLogin()){
                    if (page == 0){
                        setCollection();
                    }else if (page == 1){
                        cancelCollection();
                    }
                }else {
                    alert("请登录之后尝试");
                    ARouter.getInstance().build("/login/login").navigation();
                }
            }
        });
        
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserTools.getInstance().getIsLogin()){
                    if (TextUtils.isEmpty(comment.getText())){
                        alert("评论不能为空");
                    }else {
                        if (comment.getText().toString().length() >= 6){
                            sendComment();
                        }else {
                            alert("评论内容不足6字");
                        }
                    }
                }else {
                    alert("请登录之后尝试");
                    ARouter.getInstance().build("/login/login").navigation();
                }
            }
        });
    }

    @Override
    protected List<Message> handleHttpData(JSONObject response) {
        List<Message> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray jsonArray = response.optJSONArray("data");
            if (jsonArray != null && jsonArray.length() > 0){
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.optJSONObject(i);
                    JSONObject member = jsonArray.optJSONObject(i).optJSONObject("member");
                    Message model = new Message();
                    model.setContent(object.optString("content"));
                    model.setIds(Integer.parseInt(object.optString("id")));
                    model.setCreate_at(object.optString("create_at"));
                    if (member != null && member.length()>0){
                        model.setNickname(member.optString("nickname"));
                        model.setHead_img(member.optString("head_img"));
                    }
                    list.add(model);
                }
            }
        }
        dismissLoding();
        return list;
    }

    private void getComment() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("Tourism/tourismDetail", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                views();
                JSONObject data = response.optJSONObject("data");
                getTitleView().setContent(data.optString("title"));
                ((TextView)headView.findViewById(R.id.desc)).setText(data.optString("desc")); //景点介绍
                tourAddress = data.optString("address");
                tourlat = data.optString("lat");
                tourlng = data.optString("lng");
                ((TextView)headView.findViewById(R.id.address)).setText(data.optString("address"));
                way = data.optString("way"); //交通路线
                recommend_link = data.optString("recommend_link"); //推荐路线
                lickNumber.setText(data.optString("collection_count")); //文章收藏数量
                recommend = data.optString("recommend_link");
                JSONArray arrayIcons = data.optJSONArray("imgs");
                if (arrayIcons != null && arrayIcons.length() > 0) {
                   listUrl = new ArrayList<>();
                   for (int i = 0; i < arrayIcons.length(); i++) {
                       listUrl.add(arrayIcons.optString(i));
                   }
                }
                bindTopBannerData(listUrl);
            }
        });
    }

    //banner数据绑定
    private void bindTopBannerData(List<String> listUrl) {
        BGABanner banner = headView.findViewById(R.id.head_banner);
        banner.setAdapter(new BGABanner.Adapter<View, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, View itemView, String url, int position) {
                SimpleDraweeView simpleDraweeView = itemView.findViewById(R.id.sdv_item_fresco_content);
                simpleDraweeView.setImageURI(url);
            }
        });
        banner.setData(R.layout.homerecycle_top_banner_content, listUrl, null);
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, Message model) {
        holder.setText(R.id.name, model.getNickname());
        holder.setText(R.id.desc, model.getContent());
        holder.setText(R.id.time, model.getCreate_at());
        ((CircleImageView) holder.getView(R.id.icon)).setImageURI(model.getHead_img());
    }

    private void views(){
        BaseHttp.getInstance().query("Action/viewBlog", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if(200 == response.optInt("code")){
                    
                }else{
                    alert(response.optString("msg"));
                }
                super.onSuccess(response);
            }
        });
    }
    
    private void isCollection(){
        BaseHttp.getInstance().query("Action/isCollection", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (response.optInt("data") == 1){
                    page = 1;
                    img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_blue));
                }else {
                    page = 0;
                    img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_hui));
                }
                super.onSuccess(response);
            }
        });
    }
    
    private void setCollection(){
        BaseHttp.getInstance().query("Action/collection", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if(200 == response.optInt("code")){
                    collectionput();
                }else{
                    alert(response.optString("msg"));
                }
            }
        });
    }

    private void cancelCollection(){
        BaseHttp.getInstance().query("Action/cancelCollection", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if(200 == response.optInt("code")){
                    collectionput();
                }else{
                    alert(response.optString("msg"));
                }
            }
        });
    }
    
    private JSONObject getParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", getIntent().getStringExtra("type"));
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    private void sendComment(){
        JSONObject params = new JSONObject();
        try {
            params.put("type", getIntent().getStringExtra("type"));
            params.put("id", getIntent().getStringExtra("id"));
            params.put("content", comment.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("comment/subProductComment", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    comment.getText().clear();
                    onRefresh();
                }
                alert(response.optString("msg"));
                super.onSuccess(response);
            }
        });
    }

    private void collectionput(){
        final int collection_count = Integer.parseInt(lickNumber.getText().toString());
        img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_blue));
        if (page == 0){
            lickNumber.setText(String.valueOf(collection_count + 1));
            page = 1;
        }else if (page == 1){
            img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_hui));
            lickNumber.setText(String.valueOf(collection_count - 1));
            page = 0;
        }
    }
}
